<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

/**
 * 职位逻辑
 */
class Position extends LogicBase
{
	/**
     * 职位分类编辑
     */
    public function positionCatEdit($data = [])
    {
        
        $validate_result = $this->validatePositionCat->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatePositionCat->getError()];
        }
        
        $url = url('position_cat',array('pid'=>$data['pid']));
        
        $result = $this->modelPositionCat->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '职位分类' . $handle_text . '，catname：' . $data['catname']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelPositionCat->getError()];
    }
	 /**
     * 获取分类信息
     */
    public function getPositionCatInfo($where = [], $field = true)
    {
        
        return $this->modelPositionCat->getInfo($where, $field);
    }
	/**
     * 获取发布职位的信息
     */
    public function getWorkInfo($where = [], $field = true)
    {
        
        return $this->modelWork->getInfo($where, $field);
    }
    /**
     * 获取分类列表
     */
    public function getPositionCatList($where = [], $field = true, $order = 'sort asc', $paginate = 0)
    {
        
        return $this->modelPositionCat->getList($where, $field, $order, $paginate);
    }
    /**
     * 分类删除
     */
    public function positionCatDel($where = [])
    {
        
        $result = $this->modelPositionCat->deleteInfo($where);
        
        $result && action_log('删除', '职位分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '职位分类删除成功'] : [RESULT_ERROR, $this->modelPositionCat->getError()];
    }
	/**
     * 职位删除
     */
    public function workDel($id)
    {
        db('work')->startTrans();
        $title=db('work')->where('id',$id)->value('title');
        db('deliver_resume')->where('work_id',$id)->delete();
        db('shoucang')->where('work_id',$id)->delete();
        $result =db('work')->where('id',$id)->delete();
        if($result != false){
            action_log('删除', "职位删除，work：'".$title."'");
            db('work')->commit();
            return [RESULT_SUCCESS, '职位删除成功'];
        }else{
            db('work')->rollback();
            return [RESULT_ERROR, $this->modelWork->getError()];
        }
    }
	/**
     * 分类列表
     */
    public function getPositonCat($pid=0){
		$marr=db('position_cat')->where('pid',$pid)->select();
		if(!empty($marr)){
			foreach( $marr as $k=>$val){
				$marr[$k]['catlist']=$this->getPositonCat($val['id']);			
			}		
		}
		return $marr;
		
	}
	/**
     * 获取发布职位列表
     */
   // public function getWorkList($where = [], $field = true, $order = '', $paginate = 0)
   // {       
   //     return $this->modelWork->getList($where, $field, $order, $paginate);
   // }

	/**
     * 获取发布职位列表
     */
    public function getWorkList($where = [], $field = 'm.*,b.username,b.name,c.catname', $order = 'm.update_time desc', $paginate = DB_LIST_ROWS)
    {
        
        $this->modelWork->alias('m');
        
        $join = [
                    [SYS_DB_PREFIX . 'user b', 'm.user_id = b.id', 'LEFT'],
					[SYS_DB_PREFIX . 'position_cat c', 'm.position_id = c.id', 'LEFT'],
                ];
        
        $where['m.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelWork->join = $join;
        
        return $this->modelWork->getList($where, $field, $order, $paginate);
    }
	/**
     * 获取会员列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        !empty($data['type']) && $where['m.'.$data['type']]=1;
        isset($data['isjz']) && $data['isjz']!=9 && $where['m.isjz']=$data['isjz'];
        !empty($data['search_data']) && $where['b.name|b.username|c.catname'] = ['like', '%'.$data['search_data'].'%'];        
        !empty($data['user_id']) && $where['m.user_id']=$data['user_id'];
        return $where;
    }

}
